﻿using System;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;

namespace PsTFS.Objects
{
    /// <summary>
    /// Objet représentant un item différent
    /// </summary>
    public class ItemDiff
    {
        /// <summary>
        /// Nom du fichier
        /// </summary>
        public string Information { get; set; }
        /// <summary>
        /// Type de différence
        /// </summary>
        public TypeDiff TypeDiff { get; set; }
        /// <summary>
        /// Date de check in source
        /// </summary>
        public DateTime? DateCheckInL { get; set; }
        /// <summary>
        /// Date de check in destionation
        /// </summary>
        public DateTime? DateCheckInR { get; set; }
        /// <summary>
        /// Type d'élément
        /// </summary>
        public ItemType TypeItem { get; set; }
        /// <summary>
        /// Element soucre
        /// </summary>
        public Item ItemL { get; set; }
        /// <summary>
        /// Element desctination
        /// </summary>
        public Item ItemR { get; set; }
        /// <summary>
        /// Version source
        /// </summary>
        public VersionSpec rhsVersion { get; set; }
        /// <summary>
        /// Version destination
        /// </summary>
        public VersionSpec lhsVersion { get; set; }

        /// <summary>
        /// Permet de visualiser les différences entre deux fichiers
        /// </summary>
        public void Diff()
        {
            if (this.ItemL == null || this.ItemR == null)
            {
                Console.WriteLine("Not possible");
                return ;
            }
            else
            {
                DiffOptions optionDiff = new DiffOptions();
                optionDiff.UseThirdPartyTool = false;
                optionDiff.OutputType = DiffOutputType.Unified;
                // Wherever we want to send our text-based diff output 
                optionDiff.StreamWriter = new System.IO.StreamWriter(Console.OpenStandardOutput());

                Difference.DiffFiles(this.ItemL.VersionControlServer,
                                     Difference.CreateTargetDiffItem(this.ItemL.VersionControlServer, this.ItemL.ServerItem, lhsVersion, 0, lhsVersion),
                                     Difference.CreateTargetDiffItem(this.ItemL.VersionControlServer, this.ItemR.ServerItem, rhsVersion, 0, rhsVersion),
                                     optionDiff,
                                     VersionControlPath.GetFileName(this.ItemR.ServerItem),
                                     true);
            }
        }
    }

}
